/*
 * exame_17Set2011.c
 *
 *  Created on: Jun 9, 2012
 *      Author: ubuntu
 */


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<signal.h>

int main(int argc, char **args) {

	int n = atoi(args[1]);
	vsleep(n);
}

void tictac(int signo) {

}

void vsleep(int n) {

	pid_t pid = fork();
	if (pid == 0) {
		while (1) {
			printf("tic\n");
			kill(getpid(), SIGSTOP);
			printf("tac\n");
			kill(getpid(), SIGSTOP);

		}
	} else {

		int conta = 0;
		while (conta <= n) {
			// Voltar a armadilhar o gajo
			signal(SIGALRM, tictac);
			// Voltar a pedir ao SO, para sinalizar o escalonador daque a TEMPO segundos
			alarm(1);

			wait(NULL);

			kill(pid, SIGCONT);
			conta++;
		}
		kill(pid, SIGKILL);

	}

}
